﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using qlbansach_thuctapnhom.DAL;
using qlbansach_thuctapnhom.BLL;
namespace qlbansach_thuctapnhom.BLL
{
   public class sach_bll
    {
        quanlybansachDataContext dc = new quanlybansachDataContext();
        //==============================================
        public bool kiemtrasach(string ma)
        {
            bool ok = dc.Saches.Where(t => t.masach == ma).FirstOrDefault() != null ? true : false;
            if (!ok)
            {
                return true;
            }
            else return false;
        }
        //==============================================

        public bool sua(string ma, string ten,string gia,string manxb,string mtg,string maloai )
        {
            Sach obj = dc.Saches.Where(t => t.masach == ma).FirstOrDefault();
            try
            {
                obj.ten = ten;                
                decimal giatien;
                giatien=decimal.Parse(gia);
              
                obj.gia=giatien;
                obj.manxb=manxb;
                obj.matg=mtg;
                obj.maloai=maloai;
                dc.SubmitChanges();
                return true;
            }
            catch { return false; }

        }
        //===============================================
        public bool xoa(string ma)
        {
            Sach obj = dc.Saches.Where(t => t.masach == ma).FirstOrDefault();
            try
            {
                dc.Saches.DeleteOnSubmit(obj);
                dc.SubmitChanges();
                return true;
            }
            catch { return false; }
        }

        //==============================================
        public bool them(Sach obj)
        {
            try
            {
                dc.Saches.InsertOnSubmit(obj);
                dc.SubmitChanges();
                return true;
            }
            catch
            { return false; }
        }
        //==============================================

        public bool timkiemtensach(string ten)
        {
            bool ok = dc.Saches.Where(s=> s.ten == ten).FirstOrDefault() != null ? true : false;
            if (ok)
            {
                var tim = from s in dc.Saches where s.ten.Equals(ten) select s;
                try { return true; }
                catch { return false; }
            }
            else return false;
        }
        //==============================================
        public bool timkiemmasach(string ma)
        {
            bool ok = dc.Saches.Where(s => s.masach == ma).FirstOrDefault() != null ? true : false;
            if (ok)
            {
                var tim = from s in dc.Saches where s.ten.Equals(ma) select s;
                try { return true; }
                catch { return false; }
            }
            else return false;
        }
        //==============================================
        public bool timkiemgia(string gia)
        {
            decimal giatien;
            giatien = decimal.Parse(gia);
            bool ok = dc.Saches.Where(s => s.gia == giatien).FirstOrDefault() != null ? true : false;
            if (ok)
            {
                var tim = from s in dc.Saches where s.gia.Equals(giatien) select s;
                try { return true; }
                catch { return false; }
            }
            else return false;
        }
        //==============================================
        public bool timkiemmanxb(string ma)
        {
            bool ok = dc.Saches.Where(s => s.manxb == ma).FirstOrDefault() != null ? true : false;
            if (ok)
            {
                var tim = from s in dc.Saches where s.manxb.Equals(ma) select s;
                try { return true; }
                catch { return false; }
            }
            else return false;
        }
        //==============================================
        public bool timkiemmatacgia(string ma)
        {
            bool ok = dc.Saches.Where(s => s.matg == ma).FirstOrDefault() != null ? true : false;
            if (ok)
            {
                var tim = from s in dc.Saches where s.matg.Equals(ma) select s;
                try { return true; }
                catch { return false; }
            }
            else return false;
        }
        //==============================================
        public bool timkiemmaloai(string ma)
        {
            bool ok = dc.Saches.Where(s => s.maloai == ma).FirstOrDefault() != null ? true : false;
            if (ok)
            {
                var tim = from s in dc.Saches where s.maloai.Equals(ma) select s;
                try { return true; }
                catch { return false; }
            }
            else return false;
        }
    }
}
